package com.gxa.uhome.controller;

import com.gxa.uhome.pojo.Order;
import com.gxa.uhome.pojo.UserOrder;
import com.gxa.uhome.service.OrderService;
import com.gxa.uhome.util.ExportUtil;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Laiguowei
 * @date: 16:30 2021/6/5
 **/
@CrossOrigin
@RestController
@RequestMapping("/order")
@Api(tags = "配送订单和物流订单模快(用户申请审核之后生成物流订单和配送订单 指派骑手以后生成配送订单)" )
public class OrderController {
    @Resource
    OrderService orderService = null;

    @PostMapping("/create")
    @ApiOperation("创建物流订单")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "order对象",paramType = "body")
    )

    public JsonResult create(@RequestBody()Order order){
        JsonResult jsonResult = new JsonResult();
        Integer order1 = orderService.createOrder(order);
        if (order1!=null){
            jsonResult.setData(order1);
            jsonResult.setCode("1");
            jsonResult.setMsg("创建成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("创建失败");
        }
        return jsonResult;
    }

    @PostMapping("/update")
    @ApiOperation("用户申请审核之后生成物流订单和配送订单")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "order对象",paramType = "body")
    )

    public JsonResult update(@RequestBody()Order order){
        JsonResult jsonResult = new JsonResult();
        Integer order1 = orderService.updateOrder(order);
        if (order1!=0){
            jsonResult.setData(order1);
            jsonResult.setCode("1");
            jsonResult.setMsg("更新成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("更新失败");
        }
        return jsonResult;
    }


    @PostMapping("/selectByPage")
    @ApiOperation("订单分页查询 用于总站点分页查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map",value = "json字符串格式的map 此方法前端传入一个map map包含name（name是模糊查询部分内容），beginTime，\\n\" +\n" +
                    "            \"endTime，status，stationId，page，size 其中page，size必填项",paramType = "body")
    )

    public JsonResult selectByPage(@RequestBody() Map<String,Object> map){
        JsonResult jsonResult = new JsonResult();
        map.put("page",Integer.parseInt(String.valueOf(map.get("page"))));
        map.put("size",Integer.parseInt(String.valueOf(map.get("size"))));
        List<Map<String, Object>>  list = orderService.selectByPage(map);
        if (list!=null){
            jsonResult.setCode("1");
            jsonResult.setData(list);
            jsonResult.setMsg("成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
       return  jsonResult;
    }

    @GetMapping("/export")
    @ApiOperation("订单导出方法")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JsonResult jsonResult= new JsonResult();
        response.setContentType("application/octet-stream");
        String name = "派送订单列表";
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(name+".xls", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        HSSFWorkbook export = orderService.export(response);
                    export.write(response.getOutputStream());
                    response.getOutputStream().flush();
                    response.getOutputStream().close();

      return "成功";
    }

    @GetMapping("/getDetailByOrderId")
    @ApiOperation("通过物流订单id得到所")
    public JsonResult getDetailByOrderId(@RequestParam Integer id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        UserOrder detailByOrderId = orderService.getDetailByOrderId(id);
        if (detailByOrderId!=null){
            jsonResult.setCode("1");
            jsonResult.setData(detailByOrderId);
            jsonResult.setMsg("成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("失败");
        }
        return  jsonResult;
    }
}


